package org.example.BIO_Turn;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1",9999);
        System.out.println("客户端启动，连接到"+socket.getInetAddress());
        // 输出
        new Thread(() -> {
            OutputStream os = null;
            try {
                os = socket.getOutputStream();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            PrintStream ps = new PrintStream(os);
            Scanner sc = new Scanner(System.in);
            while (true) {
                System.out.println("请输入：");
                String msg = sc.nextLine();
                ps.println(msg);
                ps.flush();
            }
        }).start();
        // 接收服务端消息
        new Thread(() -> {
            try {
                BufferedReader bf = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String msg ;
                while ((msg = bf.readLine()) != null) {
                    System.out.println("客户端接收到服务端消息："+ msg);
                }
                bf.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }).start();
    }
}
